package com.example.discover.viewmodel;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.common.component.CategoryCtrlCom;
import com.example.common.component.GameCtrlCom;
import com.example.common.control.ICallback;
import com.example.common.model.Category;
import com.example.common.model.GameApiRankResp;
import com.example.common.model.GameResp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class DiscoverViewModel extends ViewModel {
    private static final String TAG = "DiscoverViewModel";
    // TODO: Implement the ViewModel

    private CategoryCtrlCom categoryCtrlCom;    //分类网络模块
    private GameCtrlCom gameCtrlCom;    //游戏网络模块
    private MutableLiveData<List<Category>> categories = new MutableLiveData<>();//分类数据

    private List<GameResp> games;//游戏数据
    public static Integer PN = 1;//当前页面。初始化默认为1
    public static Integer LIMIT = 15;//每页加载游戏个数，默认为15

    private MutableLiveData<Boolean> isDone = new MutableLiveData<>();
    private MutableLiveData<String> remainMsg = new MutableLiveData<>();//提示消息
    private MutableLiveData<Boolean> isAdapterLoad = new MutableLiveData<>();//是否路由刷新
    private MutableLiveData<Integer> isCount = new MutableLiveData<>();

    private Map<Integer, List<GameResp>> dd = new HashMap<>();

    public DiscoverViewModel() {
        categoryCtrlCom = new CategoryCtrlCom();
        //加载数据
        init();
    }

    private void init() {
        // 加载分类
        loadCategories();
        categoryCtrlCom = new CategoryCtrlCom();
        gameCtrlCom = new GameCtrlCom();
        games = new ArrayList<>();
        loadGames();
    }

    /**
     * 加载分类
     */
    public void loadCategories() {
        categoryCtrlCom.getCategory(new ICallback() {
            @Override
            public void onSuccess(Object response) {
                if (response == null) {
                    return;
                }
                categories.setValue((List<Category>) response);
                //isCount初始为8，拿每个分类下的八个游戏
                isCount.setValue(categories.getValue().size());
                for (int i = 0; i < categories.getValue().size(); i++) {
                    Integer id = categories.getValue().get(i).getId();
                    categoryCtrlCom.getCategoryGames(id, PN, LIMIT, new ICallback() {
                        @Override
                        public void onSuccess(Object response) {
                            GameApiRankResp resp = (GameApiRankResp) response;
                            dd.put(id, resp.getList());
                            isCount.setValue(isCount.getValue() - 1);
                            //isDone监听加载完成
                            if (Objects.equals(0, isCount.getValue())) {
                                isDone.setValue(true);
                            }
                        }

                        @Override
                        public void onFailed(String mes) {
                            isCount.setValue(isCount.getValue() - 1);
                        }
                    });
                }
            }

            @Override
            public void onFailed(String mes) {
                Log.i(TAG, "onFailed: " + mes);
            }
        });

    }

    /**
     * 加载分类下的游戏
     */
    public void loadGames() {
        //发出请求
        gameCtrlCom.getGameRank(PN, LIMIT, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                GameApiRankResp resp = (GameApiRankResp) response;
                //获取游戏数据
                if (resp.getList() != null) {
                    games.addAll(resp.getList());
                }
                isAdapterLoad.setValue(true);
            }

            @Override
            public void onFailed(String mes) {
                remainMsg.setValue(mes);
            }
        });
    }


    public MutableLiveData<List<Category>> getCategories() {
        return categories;
    }

    public Map<Integer, List<GameResp>> getDd() {
        return dd;
    }

    public List<GameResp> getGames() {
        return games;
    }

    public MutableLiveData<Boolean> getIsDone() {
        return isDone;
    }
}